package com.ruoyi.project.wx.controller;


import com.ruoyi.common.utils.bootdo.PageUtils;
import com.ruoyi.common.utils.bootdo.Query;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.common.utils.bootdo.UuidUtil;
import com.ruoyi.project.wx.domain.GoodsSpikeDO;
import com.ruoyi.project.wx.domain.GoodsSpikeTimeDO;
import com.ruoyi.project.wx.service.GoodsGoodsService;
import com.ruoyi.project.wx.service.GoodsSpikeService;
import com.ruoyi.project.wx.service.GoodsSpikeTimeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 秒杀
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-05-27 17:56:21
 */
 
@Controller
@RequestMapping("/wx/goodsSpike")
public class GoodsSpikeController {
	@Autowired
	private GoodsSpikeService goodsSpikeService;
	@Autowired
	private GoodsGoodsService goodsGoodsService;
	@Autowired
	private GoodsSpikeTimeService goodsSpikeTimeService;
	
	@GetMapping()
	@RequiresPermissions("wx:goodsSpike:goodsSpike")
	String GoodsSpike(){
	    return "wx/goodsSpike/goodsSpike";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:goodsSpike:goodsSpike")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<Map<String,Object>> goodsSpikeList = goodsSpikeService.getSpikeList(query);
		int total = goodsSpikeService.getSpikeListCount(query);
		PageUtils pageUtils = new PageUtils(goodsSpikeList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:goodsSpike:add")
	String add(){
	    return "wx/goodsSpike/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:goodsSpike:edit")
	String edit(@PathVariable("id") String id,Model model){
		GoodsSpikeDO goodsSpike = goodsSpikeService.get(id);
		Map<String,Object> map = new HashMap<String,Object>();
		List<Map<String,Object>> list = goodsGoodsService.queryAllForIdAndName(map);
		String goodsId = goodsSpike.getGoodsId();
		map.put("goodsId", goodsId);
		Map<String,Object> oldMap = goodsGoodsService.queryAllForIdAndName(map).get(0);
		map.clear();
		map.put("delFlag","0");
		List<GoodsSpikeTimeDO> timeList = goodsSpikeTimeService.list(map);
		model.addAttribute("goodsList", list);
		model.addAttribute("goodsSpike", goodsSpike);
		model.addAttribute("oldMap", oldMap);
		model.addAttribute("timeList", timeList);
	    return "wx/goodsSpike/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:goodsSpike:add")
	public R save(GoodsSpikeDO goodsSpike){
		Date date = new Date();
		goodsSpike.setId(UuidUtil.get32UUID());
		goodsSpike.setDelFlag(0);
		goodsSpike.setCreateAt(date);
		goodsSpike.setNumSale(0);
		if(goodsSpikeService.save(goodsSpike)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:goodsSpike:edit")
	public R update( GoodsSpikeDO goodsSpike){
		goodsSpikeService.update(goodsSpike);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:goodsSpike:remove")
	public R remove( String id){
		if(goodsSpikeService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:goodsSpike:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		goodsSpikeService.batchRemove(ids);
		return R.ok();
	}
	
}
